<template>
  <div>

    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>系统</el-breadcrumb-item>
      <el-breadcrumb-item>资源权限</el-breadcrumb-item>
    </el-breadcrumb>

    <el-form :inline="true" class="query-form" label-width="6rem">
      <el-form-item label="业务系统">
        <el-select v-model="selectedSystem" value-key="_id" placeholder="请选择业务系统">
          <el-option v-for="item in getSystems" :key="item._id" :label="item.sysName" :value="item">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onQuery">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onAdd">新增</el-button>
      </el-form-item>
    </el-form>

    <el-table class="table" :data="getPermissions" border stripe style="width: 100%">
      <el-table-column prop="permissionDescription" label="资源描述">
      </el-table-column>
      <el-table-column prop="permissionType" label="访问类型">
      </el-table-column>
      <el-table-column prop="permissionUrl" label="资源地址">
      </el-table-column>
      <el-table-column label="操作" min-width="60">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<style>
.query-form {
  margin-top: 20px;
  padding-top: 25px;
  background-color: #f2f2f2;
}

.table {
  margin-top: 30px;
}

.pagination {
  margin-top: 30px;
  text-align: right;
  padding-right: 2rem;
}
</style>

<script>
import { mapGetters, mapActions, mapMutations } from "vuex";
import { SET_PERMISSIONS } from "../../store/mutation-types";

export default {
  name: "Permissions",
  created() {
    console.log("hmx^^^^^^^|created");
    this.reqGetSystems();
    this[SET_PERMISSIONS]({ permissions: [] });
  },
  computed: {
    ...mapGetters(["getSystems", "getPermissions"])
  },
  data() {
    return {
      selectedSystem: undefined
    };
  },
  methods: {
    ...mapMutations([SET_PERMISSIONS]),
    ...mapActions(["reqGetSystems", "reqGetPermissions", "reqDeletePermission"]),
    onQuery() {
      console.log("onQuery!");
      if (this.selectedSystem) {
        debugger;
        this.reqGetPermissions({ sysId: this.selectedSystem._id });
      } else {
        this.$message({
          message: "请选择一个业务系统。",
          duration: 1000,
          type: "warning"
        });
      }
    },
    onAdd() {
      console.log("onAdd!");
      if (this.selectedSystem) {
        console.log(this.selectedSystem);
        debugger;
        this.$router.push({
          name: "AddPermission",
          params: {
            sysId: this.selectedSystem._id,
            sysName: this.selectedSystem.sysName
          }
        });
      } else {
        this.$message({
          message: "请选择一个业务系统。",
          duration: 1000,
          type: "warning"
        });
      }
    },
    handleEdit(index, row) {
      console.log(index);
      console.log(row);
      this.$router.push({
        name: "EditPermission",
        params: {
          sysId: this.selectedSystem._id,
          sysName: this.selectedSystem.sysName,
          permissionId: row._id,
          permissionDescription: row.permissionDescription,
          permissionType: row.permissionType,
          permissionUrl: row.permissionUrl
        }
      });
    },
    handleDelete(index, row) {
      this.$confirm("此操作将永久删除该资源权限, 是否继续?", "提示", {
        confirmButtonText: "确定",
        confirmButtonClass: "el-button--danger",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.reqDeletePermission({
            sysId: this.selectedSystem._id,
            permissionId: row._id
          });
        })
        .catch(() => {});
    }
  }
};
</script>
